package com.net;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by yangliwei on 2017/7/23.
 */
public class URLDemo {

    /**
     * 目测URL参数protocal 有ftp, http, nntp,flie
     *
     * 该方法新建一个基于file协议的url连接，
     */
    @Test
    public void testURL(){
        String protocal = "file";
        String host = "127.0.0.1";
        String file = System.getProperty("user.dir");
        try {
            URL url = new URL(protocal,host, File.separator+file);
//            file://127.0.0.1\D:\ideaProject\javaStudy
            System.out.println(url.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testHTTP(){
        String path = "http://192.168.0.110:8080/vedios/test.html";
        try {
            URL url  = new URL(path);
            URLConnection connection = url.openConnection();
            InputStream inputStream = connection.getInputStream();
            InputStreamReader reader = new InputStreamReader(inputStream,"UTF-8");
            char[]result = new char[20];
            while (reader.read(result)!=-1) {
                System.out.println(result);
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
